/*
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Oracle or the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */ 

/*
 * Swing version
 */

import javax.swing.*;

import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class UserAdmin extends JPanel implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	
	JButton BT_Connect;
	JButton BT_CreateUser;
	JButton BT_Delete;
	JTextField TF_UserName;
	
	public UserAdmin() {
		
		super(new GridBagLayout());
		GridBagLayout gridbag = (GridBagLayout)getLayout();
		GridBagConstraints c = new GridBagConstraints();
		
		JLabel LB_UserName = new JLabel( "User Name" );
		c.gridx = 0;
		c.gridy = 0;
		add( LB_UserName, c );
		
		TF_UserName = new JTextField( "EMUEEE" );
		TF_UserName.setEditable( true );
		c.gridx = 1;
		c.gridy = 0;
		add( TF_UserName, c );
		
		BT_CreateUser = new JButton( "Create User" );
		c.gridx = 2;
		c.gridy = 0;
		add( BT_CreateUser, c );
		
		BT_Connect = new JButton( "Connect" );
		c.gridx = 0;
		c.gridy = 1;
		gridbag.setConstraints( BT_Connect, c );
		add( BT_Connect );
		
		BT_Delete = new JButton( "Delete" );
		c.gridx = 1;
		c.gridy = 1;
		gridbag.setConstraints( BT_Delete, c );
		add( BT_Delete );
		
		BT_CreateUser.addActionListener( this );
		
		BT_Connect.addActionListener( new ConnectClick( TF_UserName ) );
		
		BT_Delete.addActionListener( new DeleteClick( TF_UserName ) );
		
	}
	
	public void actionPerformed( ActionEvent e ) {
		
		try {
			
			String username = TF_UserName.getText();
			
			Process p = Runtime.getRuntime().exec( new String[] { "sudo", "./UserCreate", username } );
			
			BufferedReader in = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
			String line = null;
			
			while( ( line = in.readLine() ) != null ) {
				JOptionPane.showMessageDialog( null, line );
			}
			
		} catch( IOException ex ) {
			ex.printStackTrace();
		}
		
	}
	
	/**
	* Create the GUI and show it.  For thread safety,
	* this method should be invoked from the
	* event-dispatching thread.
	*/
	private static void createAndShowGUI() {
		//Create and set up the window.
		JFrame frame = new JFrame( "User Administration" );
		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		
		//Create and set up the content pane.
		JComponent newContentPane = new UserAdmin();
		newContentPane.setOpaque( true ); //content panes must be opaque
		frame.setContentPane( newContentPane );
		
		//Display the window.
		frame.pack();
		frame.setVisible(true);
		
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		javax.swing.SwingUtilities.invokeLater(new Runnable() {	public void run() {	createAndShowGUI();	} }	);
		
	}
	
}

class ConnectClick implements ActionListener {
	
	JTextField TF_UserName;
	
	public ConnectClick( JTextField TF) {
		TF_UserName = TF;
	}
	
	public void actionPerformed( ActionEvent e ) {
		
		try {
			Runtime.getRuntime().exec( new String[] { "./UserConnect", TF_UserName.getText() } );
			
		} catch( IOException ex ) {
			ex.printStackTrace();
		}
	}
	
}

class DeleteClick implements ActionListener {
	
	JTextField TF_UserName;
	
	public DeleteClick( JTextField TF) {
		TF_UserName = TF;
	}
	
	public void actionPerformed( ActionEvent e ) {
		
		int YesNo = JOptionPane.showConfirmDialog( null, "Delete " + TF_UserName.getText() + "?", "Confirm", JOptionPane.YES_NO_OPTION );
		
		if( YesNo == 0 ) {
			
			try {
				Runtime.getRuntime().exec( new String[] { "sudo", "pkill", "-9", "-U", TF_UserName.getText() } );
			} catch( IOException ex1 ) {
				ex1.printStackTrace();
				
			}
			
			JOptionPane.showMessageDialog( null, "Killing the process of User " + TF_UserName.getText() + "......" );
			
			// Hold for a while till all process were terminated
			try {
				Thread.sleep(1000);
				
			} catch( InterruptedException ex2 ) {
				ex2.printStackTrace();
				
			}
			
			try {
				Runtime.getRuntime().exec( new String[] { "sudo", "userdel", "-r", TF_UserName.getText() } );
			} catch( IOException ex3 ) {
				ex3.printStackTrace();
				
			}
			
		}
		
	}
	
}
